﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace ODS.Utils
{
    public class AnimationUtil
    {

        public event Action<bool> OnAnimationCompleted;

        static AnimationUtil instance;

        public static AnimationUtil getInstance()
        {
            if (instance == null)
            {
                instance = new AnimationUtil();
  
            }

            return instance;

        }

        //DoubleAnimation fakeMoveUpAnim;
        //DoubleAnimation fakeMoveDownAnim;

        bool isScrollDown;

        public  void startAnimation(Control control, int fromY, int toY)
        {
            if (fromY > toY)
                isScrollDown = false;
            else
                isScrollDown = true;

            Storyboard storyboard = new Storyboard();
            storyboard.Completed += storyboard_Completed;

            DoubleAnimation moveAnim = new DoubleAnimation();
            //fakeMoveUpAnim = new DoubleAnimation();
            //fakeMoveDownAnim = new DoubleAnimation();

            storyboard.Children.Add(moveAnim);
            //storyboard.Children.Add(fakeMoveUpAnim);
            //storyboard.Children.Add(fakeMoveDownAnim);

            Storyboard.SetTarget(moveAnim, control);
            Storyboard.SetTargetProperty(moveAnim, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)"));

            //Storyboard.SetTarget(fakeMoveUpAnim, control);
            //Storyboard.SetTargetProperty(fakeMoveUpAnim, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)"));

            //Storyboard.SetTarget(fakeMoveDownAnim, control);
            //Storyboard.SetTargetProperty(fakeMoveDownAnim, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)"));
            
            TranslateTransform trans = new TranslateTransform() { X = 1.0, Y = 1.0 };
            control.RenderTransformOrigin = new Point(0.5, 0.5);
            control.RenderTransform = trans;

            int miliSecond = 300;

            
            moveAnim.Duration = TimeSpan.FromMilliseconds(miliSecond);
            moveAnim.From = fromY;
            moveAnim.To = toY;

            //fakeMoveUpAnim.Duration = TimeSpan.FromMilliseconds(miliSecond + 50);
            //fakeMoveUpAnim.From = toY;
            //fakeMoveUpAnim.To = toY - 20;

            //fakeMoveUpAnim.Duration = TimeSpan.FromMilliseconds(miliSecond + 50);
            //fakeMoveUpAnim.From = toY - 20;
            //fakeMoveUpAnim.To = toY ;
            
            storyboard.Begin();
        }

        void storyboard_Completed(object sender, EventArgs e)
        {
            OnAnimationCompleted(isScrollDown);
        }
    }
}
